package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.VO.DetentionMachineBoundVO;
import com.tbit.main.pojo.VO.StockMachineVO;
import com.tbit.main.service.DetentionMachineBoundService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 扣押车辆
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 15:55 2023/2/8
 */
@RestController
@RequestMapping("/detentionMachine")
public class DetentionMachineBoundController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private DetentionMachineBoundService detentionMachineBoundService;



    /**
     * 通过车辆编号获取
     *
     * @param userCode  车辆编号
     * @param accountId 区域id
     * @param token
     * @return
     */
    @RequestMapping("/getByUserCode")
    public JsonResult getByUserCode(String userCode, Integer accountId, String token) {
        if (accountId == null) {
            AccountUser accountUser = tokenService.getAccount(token);
            accountId = accountUser.getAccountId();
        }
        List<DetentionMachineBoundVO> list = detentionMachineBoundService.getByUserCode(userCode, accountId);
        return JsonResult.succ(list);
    }

    /**
     * 批量新增
     *
     * @param userCodes 车辆编号集合、多个用逗号分割
     * @param addTime   扣押时间
     * @param remark    备注
     * @return
     */
    @RequestMapping("/addBatch")
    @OperateLog(module = ModuleEnum.DETENTIONMACHINE, methods = MethodEnum.DETENTIONMACHINE_ADD)
    public JsonResult addBatch(String userCodes, Long addTime, String remark, String token, Integer detentionId,String detentionName,Integer accountId) {
        Assert.hasLength(userCodes, "Machine.userCode.notNull");
        Assert.notNull(addTime, "AccountEvent.control.accountEventTime.notNull");

        AccountUser accountUser = tokenService.getAccount(token);
        String stringList = detentionMachineBoundService.addBatch(userCodes, addTime, remark, accountUser, detentionId,detentionName,accountId);
        return JsonResult.succ(stringList);
    }

    /**
     * 删除
     *
     * @param userCode 车辆编号
     * @param token
     * @return
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.DETENTIONMACHINE, methods = MethodEnum.DETENTIONMACHINE_DEL)
    public JsonResult del(String userCode, String token) {
        Assert.hasLength(userCode, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        detentionMachineBoundService.del(userCode, accountUser);
        return JsonResult.succ();
    }

    /**
     *  查询所有的扣押区的扣押车辆输数量
     */
    @RequestMapping("/getByDetentionId")
    public JsonResult getByDetentionId(Integer detentionId,String userCode) {
        List<DetentionMachineBoundVO> list = detentionMachineBoundService.getByDetentionIdAndUserCode(detentionId,userCode);
        return JsonResult.succ(list);
    }

    /**
     *  根据扣押区id查询扣押车辆
     */
    @RequestMapping("/selectByDetentionId")
    public JsonResult selectByDetentionId(@RequestParam(name="detentionId",required = false) List<Integer> detentionId,String userCode,Integer accountId) {
        List<DetentionMachineBoundVO> stockMachineVOS = detentionMachineBoundService.selectByDetentionId(detentionId,userCode,accountId);
        return JsonResult.succ(stockMachineVOS);
    }


    /**
     * 批量更换扣押区
     *
     * @param userCodes 车辆编号
     * @param token     鉴权秘钥
     */
    @RequestMapping("/updateBatch")
    public JsonResult updateBatch(@RequestParam("userCodes") List<String> userCodes, String token, String detentionName,String detentionId) {
        Assert.notNull(userCodes, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        detentionMachineBoundService.updateBatch(userCodes, accountUser,detentionName,detentionId);
        return JsonResult.succ();
    }

    /**
     * 删除
     *
     * @param userCode 车辆编号
     * @param token
     * @return
     */
    @RequestMapping("/delMul")
    @OperateLog(module = ModuleEnum.DETENTIONMACHINE, methods = MethodEnum.DETENTIONMACHINE_DEL)
    public JsonResult delMul(@RequestParam("userCodeS") List<String> userCodeS, String token) {
        AccountUser accountUser = tokenService.getAccount(token);
        detentionMachineBoundService.delMul(userCodeS, accountUser);
        return JsonResult.succ();
    }
}
